# 导入tushare
import csv

import tushare as ts


# 初始化pro接口
token = "bb4d05bd560a3e3e4ef336de9e80b860568a6a51dadeb797ccdf73f0"

pro = ts.pro_api(token)

# 1. 获取上交所/深交所/北交所证券代码
def getCodeList():
    codeList = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')
    return codeList

def getPriceList(code):
    # # 拉取数据
    # df = pro.daily(**{
    #     "ts_code": "301091.sh",
    #     "trade_date": "",
    #     "start_date": 20180701,
    #     "end_date": 20180718,
    #     "offset": "",
    #     "limit": ""
    # }, fields=[
    #     "ts_code",
    #     "trade_date",
    #     "open",
    #     "high",
    #     "low",
    #     "close",
    #     "pre_close",
    #     "change",
    #     "pct_chg",
    #     "vol",
    #     "amount"
    # ])
    # return df

    # 东财数据

    ts.set_token(token)
    df = ts.realtime_quote(ts_code='600000.SH', src='dc')
    return df






if __name__ == "__main__":

    codeList = getCodeList()
    with open('code_list.csv', 'w', newline='') as f:
        writer = csv.writer(f, delimiter=',')
        for item in codeList:
            print(item)
            writer.writerows(item)
        f.close()
    # print(codeList)
    for codeItem in codeList:
        ts_code = codeItem
        pd =  getPriceList(ts_code)
        print(pd)




